package com.ruoyi.project.platform.controller;

import java.util.List;

import com.ruoyi.project.platform.dto.StrategyDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.platform.domain.StyStrategy;
import com.ruoyi.project.platform.service.IStyStrategyService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.annotation.security.PermitAll;

/**
 * 策略Controller
 *
 * @author ruoyi
 * @date 2020-07-23
 */
@RestController
@RequestMapping("/platform/strategy")
@Api("策略管理")
public class StyStrategyController extends BaseController {
    @Autowired
    private IStyStrategyService styStrategyService;

    /**
     * 查询策略列表
     */
    @PreAuthorize("@ss.hasPermi('platform:strategy:list')")
    @GetMapping("/list")
    public TableDataInfo list(StyStrategy styStrategy) {
        startPage();
        List<StyStrategy> list = styStrategyService.selectStyStrategyList(styStrategy);
        return getDataTable(list);
    }

    /**
     * 导出策略列表
     */
    @PreAuthorize("@ss.hasPermi('platform:strategy:export')")
    @Log(title = "策略", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StyStrategy styStrategy) {
        List<StyStrategy> list = styStrategyService.selectStyStrategyList(styStrategy);
        ExcelUtil<StyStrategy> util = new ExcelUtil<StyStrategy>(StyStrategy.class);
        return util.exportExcel(list, "strategy");
    }

    /**
     * 获取策略详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:strategy:query')")
    @GetMapping(value = "/{strategyId}")
    public AjaxResult getInfo(@PathVariable("strategyId") Long strategyId) {
        return AjaxResult.success(styStrategyService.selectStyStrategyById(strategyId));
    }

    /**
     * 新增策略
     */
    @PreAuthorize("@ss.hasPermi('platform:strategy:add')")
    @Log(title = "策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StyStrategy styStrategy) {
        return toAjax(styStrategyService.insertStyStrategy(styStrategy));
    }

    /**
     * 修改策略
     */
    @PreAuthorize("@ss.hasPermi('platform:strategy:edit')")
    @Log(title = "策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StyStrategy styStrategy) {
        return toAjax(styStrategyService.updateStyStrategy(styStrategy));
    }

    /**
     * 删除策略
     */
    @PreAuthorize("@ss.hasPermi('platform:strategy:remove')")
    @Log(title = "策略", businessType = BusinessType.DELETE)
    @DeleteMapping("/{strategyIds}")
    public AjaxResult remove(@PathVariable Long[] strategyIds) {
        return toAjax(styStrategyService.deleteStyStrategyByIds(strategyIds));
    }

    /**
     * 删除策略
     */
    @ApiOperation("策略同步参数")
    @PostMapping("/sync")
    public AjaxResult sync(@RequestBody StrategyDto.SyncInfo info) {
        styStrategyService.syncParameter(info);
        return AjaxResult.success();
    }
}
